import '../style/less/index.less'

import $ from 'jquery'
import { tns } from 'tiny-slider/src/tiny-slider.module'

if($('.slider').length!=0){
    let slider = tns({
        container: '.slider',
        autoplay: true,
        items: 1,
        controls: false,
        autoplayButtonOutput: false,
        speed: 500,
    })
    slider.events.on('indexChanged', info => {
    })
}
//活动详情页
var index1=0,index2=0;
$('.e_title').click(function()
{
    $(this).siblings().removeClass("blue_color")
    $(this).addClass("blue_color")
    $('html,body').animate(
        {
            scrollTop:$($(this).attr('data-plne')).offset().top-130
        }, 200);

});
$(window).scroll(function(event){
    for(var i=0;i<$('.mbody').length;i++){
        if($('.mbody').eq(i).offset().top-$(window).scrollTop()<133){
            index2=i;
        }
    }
    if(index1!=index2){
        $('.e_title').removeClass("blue_color")
        $('.e_title').eq(index2).addClass("blue_color")
        $('.cursor').animate({left:index2*140},200)
        index1=index2
    }
});
$(window).scroll(function(event){
    if($(window).scrollTop()>345){
        $(".top-second-nav").css('position','fixed')
        $(".top-second-nav").css('top','70px')
    }
    else{
        $(".top-second-nav").css('position','absolute')
        $(".top-second-nav").css('top','335px')
    }
});
$('.scroll').on('click',function(e){
    e.preventDefault();
    window.location.href=setParam('scrollTop',$(window).scrollTop(),$(this).attr('href'));
})
//获得浏览器参数
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}
function setParam(param,value,url){
    var query = url.split('?')[1]
    var p = new RegExp("(^|&"+param+")=[^&]*");
    var returnUrl=''
    if(p.test(query)){
        query = query.replace(p,"$1="+value);
        returnUrl = url.split('?')[0]+'?'+query;
    }else{
        if(query == ''){
            returnUrl = url.split('?')[0]+'?'+param+'='+value;
        }else{
            returnUrl = url.split('?')[0]+'?'+query+'&'+param+'='+value;
        }
    }
    return returnUrl
}
//展览中心详情页
window.onload=function(){
    if(getUrlParam('scrollTop')!=null){
        $('html,body').scrollTop(getUrlParam('scrollTop'));
    }
    if($(".intro-body").height()>80){
        $(".intro-body").css("height","80px");
        $(".intro-more").css("display","block");
    }
    getNotifyNumber()
    getUserLoginState()
    var location = window.location.href
    var index = location.indexOf("activity/detail")
    if(index>=0){
        isActivityFav()
        isActivityApply()
    }
}
$("#more").click(()=>{
    $(".intro-body").css("height","auto");
    $("#more").css("display","none");
    $("#less").css("display","block");
});
$("#less").click(()=>{
    $(".intro-body").css("height","90px");
    $("#more").css("display","block");
    $("#less").css("display","none");
});
$('#activityApply').on('click',function(){
    if($('#activityApply').html()=="立即报名"){
        var location = window.location.href
        var index = location .lastIndexOf("\/");
        location = location .substring(index + 1, location .length);
        window.location.href="/console/activity/applyActivity/"+location
    }
})
$('#activityFav').on('click',function(){
    var location = window.location.href
    var index = location .lastIndexOf("\/");
    location = location .substring(index + 1, location .length);
    $.ajax({
        url: "/api/activity/favorite?t="+new Date().getTime(),		//请求地址
        type : "POST",
        dataType : 'json',
        data:{
            favoriteId:location
        },
        contentType:"application/x-www-form-urlencoded;charset=UTF-8",
        success : function(data) {
            if (data.status.code== 1) {
                $('#activityFav').html('<img src="/style/img/shoucang.png">'+data.status.message)
            }
            else if(data.status.code==2){
                window.location.href=""+"?"+window.location.href
            }
        }
    });
})
function isActivityApply(){
    var location = window.location.href
    var index = location .lastIndexOf("\/");
    location = location .substring(index + 1, location .length);
    $.ajax({
        url: "/api/common/apply/status/"+location+"&t="+new Date().getTime(),		//请求地址
        type : "GET",
        dataType : 'json',
        contentType:"application/x-www-form-urlencoded;charset=UTF-8",
        success : function(data) {
            if (data.status.code== 2002) {
                $('#activityApply').html("已报名")
            }
            else{
                isActivityFull()
            }
        }
    });
}
function isActivityFav(){
    var location = window.location.href
    var index = location .lastIndexOf("\/");
    location = location .substring(index + 1, location .length);
    $.ajax({
        url: "/api/common/favorite/status?favoriteId="+location+"&type=activity"+"&t="+new Date().getTime(),		//请求地址
        type : "GET",
        dataType : 'json',
        contentType:"application/x-www-form-urlencoded;charset=UTF-8",
        success : function(data) {
            if (data.status.code== 2004) {
                $('#activityFav').html('<img src="/style/img/shoucang.png">'+"已收藏")
            }
        }
    });
}
function isActivityFull(){
    var location = window.location.href
    var index = location .lastIndexOf("\/");
    location = location .substring(index + 1, location .length);
    $.ajax({
        url: "/api/activity/apply/full/"+location+"&t="+new Date().getTime(),		//请求地址
        type : "GET",
        dataType : 'json',
        contentType:"application/x-www-form-urlencoded;charset=UTF-8",
        success : function(data) {
            if (data.status.code== 2006) {
                $('#activityApply').html("名额已满")
                $('#activityApply').css('backgroundColor', '#999');
            }
        }
    });
}
function getUserLoginState(){
    $.ajax({
        url: "/api/common/login/status?t="+new Date().getTime(),		//请求地址
        type : "GET",
        dataType : 'json',
        contentType:"application/x-www-form-urlencoded;charset=UTF-8",
        success : function(data) {
            if (data.status.code== 1) {
                $('.namespace').html(data.result)
                $('#login_li').addClass("none")
                $('#register_li').addClass("none")
                $('#username_li').removeClass("none")
                $('#notify_li').removeClass('none')
            }
        }
    });
}
function getNotifyNumber(){
    $.ajax({
        url: "/api/notify/count?t="+new Date().getTime(),		//请求地址
        type : "GET",
        dataType : 'json',
        contentType:"application/x-www-form-urlencoded;charset=UTF-8",
        success : function(data) {
            if (data.status.code== 1) {
                $('#notifyNumber').html(data.result)
            }
        }
    });
}
$('#login_li').on('click',function(){
    window.location.href="/sign/login?from="+window.location.href
})
$('#register_li').on('click',function(){
    window.location.href="/sign/register?from="+window.location.href
})

